package class1.s2022350143027.utils;

import java.io.*;

public class YzyFileUtils extends base.utils.FileUtils{

    @Override
    public void add(String filename) {
        File file=new File("d:\\2022350143027\\yzy_24511958.txt");
        if(file.exists()){
            file.delete();
        }else{
            try {
                System.out.println(file.createNewFile());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public void copy(String filename, String filename2)  {
        //这里的路径可以把最后改成对应的文件名
        InputStream in = null;
        try {
            in = new FileInputStream("d:\\2022350143027\\yzy_24511958.txt");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        //build io class
        OutputStream out = null;
        try {
            out = new FileOutputStream("d:\\2022350143027\\yzy_24511958_copy.txt");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }


        byte[] buff = new byte[1024]; // 定义一个字节数组，作为缓冲区

        //记录长度
        int cnt=0;

        //当前时间，用于计算时间差
        long begintime = System.currentTimeMillis();

        while (true) {
            try {
                if (!((cnt = in.read(buff)) != -1)) break;
            } catch (IOException e) {
                throw new RuntimeException(e);
            } // 读取一个字节并判断是否读到文件末尾
            try {
                out.write(buff,0,cnt); // 将读到的字节写入文件
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }

        long endtime = System.currentTimeMillis();

        System.out.println("共花费"+ (endtime - begintime) +"毫秒");

        try {
            in.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        try {
            out.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public void delete(String filename) {
        File file=new File("d:\\2022350143027\\yzy_24511958.txt");
        if (file.getParentFile().exists()) { //判断file是不是存在
            file.getParentFile().mkdir();
        }

        if(file.exists()){
            file.delete();
        }else{
            System.out.println("i am yzy 2022350143027,要删除的文件不能存在");
        }

    }

    @Override
    public void edit(String filename, String content) {
        //build a great of yzy io class
        OutputStream out = null;
        try {
            out = new FileOutputStream("d:\\2022350143027\\yzy_24511958.txt");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        String yzyStr="hello world~";
        byte[] yzyArr=yzyStr.getBytes();
        for (int i=0;i<yzyArr.length;i++){
            try {
                out.write(yzyArr[i]);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        try {
            out.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
